package com.justyh.ssm.controller;

import com.beust.jcommander.internal.Lists;
import com.justyh.ssm.util.QRCodeUtils;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.*;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

/**
 * Created by han.yuan on 2017/1/11.
 */
@Controller
@RequestMapping("/qrcode")
public class QRCodeController {
    private List<Restaurant> restaurantList;

    @RequestMapping(path={"","/"},method = {RequestMethod.GET})
    public Object index(){
        return new ModelAndView("qrcode");
    }
    @RequestMapping(value="getCode",method = RequestMethod.GET)
    public void getQRCode( HttpServletRequest request,
                            HttpServletResponse response){
        String shopURL = randomDishes();
        //二维码图片输出流
        OutputStream out = null;
        try{
            response.setContentType("image/jpeg;charset=UTF-8");
            BufferedImage image = QRCodeUtils.createQRCode(shopURL);
            //实例化输出流对象
            out = response.getOutputStream();
            //画图
            ImageIO.write(image, "png", response.getOutputStream());
            out.flush();
            out.close();
        }catch (Exception e){
            log.println("生成二维码出现异常"+e.getMessage());
            e.printStackTrace();
        }finally {
            try{
                if (null != response.getOutputStream()) {
                    response.getOutputStream().close();
                }
                if (null != out) {
                    out.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    private  String randomDishes(){

        int i= new Random().nextInt(4);
        StringBuilder resultsb=new StringBuilder();
        Restaurant restaurant=restaurantList.get(i);

        String restName=restaurant.getRestName();
        resultsb=resultsb.append("今日推荐：").append(restName).append("\n");
        Map<String,List<String>> favoriteDishMap=restaurant.getFavoriteDishes();
        for(Map.Entry<String,List<String>> entry:favoriteDishMap.entrySet()){
            resultsb.append(entry.getKey()).append(":")
                    .append(entry.getValue()
                    .get(new Random().nextInt(entry.getValue().size())))
                    .append("\n");
        }
        return resultsb.toString();

    }
    @PostConstruct
    public void init(){
        restaurantList=new ArrayList<Restaurant>();
        Restaurant restaurant1=new Restaurant();
        restaurant1.setRestName("煲仔饭");
        Map<String,List<String>> favoriteDishes1=new HashMap<String, List<String>>();
        favoriteDishes1.put("高桐",Lists.newArrayList("小炒千页豆腐","香菇滑鸡","面筋"));
        favoriteDishes1.put("袁汉",Lists.newArrayList("小炒千页豆腐","香菇滑鸡","面筋"));
        favoriteDishes1.put("王立龙",Lists.newArrayList("腊味全拼","腌鱼"));
        favoriteDishes1.put("李尚蔚",Lists.newArrayList("外卖"));
        restaurant1.setFavoriteDishes(favoriteDishes1);

        Restaurant restaurant2=new Restaurant();
        restaurant2.setRestName("五谷鱼粉");
        Map<String,List<String>> favoriteDishes2=new HashMap<String, List<String>>();
        favoriteDishes2.put("高桐",Lists.newArrayList("五谷鱼粉","羊肉汤+饼","西红柿炒蛋盖浇饭","农家小炒肉"));
        favoriteDishes2.put("袁汉",Lists.newArrayList("五谷鱼粉","羊肉汤+饼","西红柿炒蛋盖浇饭","农家小炒肉"));
        favoriteDishes2.put("王立龙",Lists.newArrayList("长长长豆腐","猪肝","辣子鸡丁"));
        favoriteDishes2.put("李尚蔚",Lists.newArrayList("外卖"));
        restaurant2.setFavoriteDishes(favoriteDishes2);

        Restaurant restaurant3=new Restaurant();
        restaurant3.setRestName("牛肉拉面");
        Map<String,List<String>> favoriteDishes3=new HashMap<String, List<String>>();
        favoriteDishes3.put("高桐",Lists.newArrayList("拉面","大盘鸡"));
        favoriteDishes3.put("袁汉",Lists.newArrayList("一碗拉面"));
        favoriteDishes3.put("王立龙",Lists.newArrayList("拉面"));
        favoriteDishes3.put("李尚蔚",Lists.newArrayList("外卖"));
        restaurant3.setFavoriteDishes(favoriteDishes3);

        Restaurant restaurant4=new Restaurant();
        restaurant4.setRestName("要饭");
        Map<String,List<String>> favoriteDishes4=new HashMap<String, List<String>>();
        favoriteDishes4.put("高桐",Lists.newArrayList("打狗棒","破碗","破衣服"));
        favoriteDishes4.put("袁汉",Lists.newArrayList("化斋"));
        favoriteDishes4.put("王立龙",Lists.newArrayList("算命"));
        favoriteDishes4.put("李尚蔚",Lists.newArrayList("外卖"));
        restaurant4.setFavoriteDishes(favoriteDishes4);

        restaurantList.add(restaurant1);
        restaurantList.add(restaurant2);
        restaurantList.add(restaurant3);
        restaurantList.add(restaurant4);
    }

    @Data
    private static class Restaurant{
        private String restName;
        private Map<String,List<String>> favoriteDishes;
    }
    public static void main(String[] args){
        QRCodeController qrCodeController=new QRCodeController();
        qrCodeController.init();
        System.out.println(qrCodeController.randomDishes());
    }
}
